<template>
    <div class="ele-body">
      <!-- 搜索表单 -->
      <saleoutorderSearch @search="reload"  />
      <!-- 数据表格 -->
      <el-card shadow="never">
        <ele-pro-table
          ref="table"  row-key="id" :columns="columns" :datasource="datasource"  :need-page="true" :cell-style="cellStyle"  @done="onDone"  :height="tableHeight" :full-height="tableFullHeight"
        >
          <!-- 表头工具栏 -->
          <template slot="toolbar">
            <el-button
              v-permission="'SaleOutOrder_Add'"
              size="small"
              type="primary"
              icon="el-icon-plus"
              class="ele-btn-icon"
              @click="openEdit({},1)"
            >
              添加
            </el-button>
            <el-button
              v-permission="'SaleOutOrder_Export'"
              size="small"
              icon="el-icon-upload2"
              class="ele-btn-icon"
              @click="reportexcel"
            >
              导出
            </el-button>
            <div class="ele-space" style="float: right; margin-top: 5px;">
              <el-button type="danger"  circle size="mini"></el-button>未出库
              <el-button type="success"  circle size="mini"></el-button>已出库
              <!-- <el-button type="primary"  circle size="mini"></el-button>出库中 -->
              <!-- <div class="list-tool-divider" style="margin-right: 5px;margin-left: 5px;">
                <el-divider direction="vertical" />
              </div>
              <div class="list-tool-item" style="margin-right: 5px;">
                <span>作废数据</span>&nbsp;
                <el-switch
                  :active-value= 1
                  :inactive-value= 0
                  v-model="isDeleted"
                  @change="editState"
                  >
            </el-switch>
              </div> -->
              <div class="list-tool-divider" style="margin-right: 5px;">
                <el-divider direction="vertical" />
              </div>
          </div>
          </template>
          <!-- 标题列 -->
          <template slot="outOrderNo" slot-scope="{ row }">
            <el-tag type="danger" v-if="row.outStatus==1">{{ row.outOrderNo }}</el-tag>
            <el-tag type="success" v-else-if="row.outStatus==5" >{{ row.outOrderNo }}</el-tag>
            <el-tag type="primary" v-else >{{ row.outOrderNo }}</el-tag>
          </template>
          <!-- 标题列 -->
          <template slot="status" slot-scope="{ row }">
            <el-tag type="danger" v-if="row.status==1">{{ row.statusDesc }}</el-tag>
            <el-tag type="success" v-else-if="row.status==5">{{ row.statusDesc }}</el-tag>
          </template>
           <!-- 标题列 -->
           <template slot="orderNo" slot-scope="{ row }">
            <el-link  type="primary"   @click="clickbillNo(row)" >{{ row.orderNo?row.orderNo +'('+row.batchNo+')':'无' }}</el-link>
          </template>
           <!-- 标题列 -->
           <template slot="title" slot-scope="{ row }">
            ￥{{ row.purPrice }}
          </template>
           <!-- 操作列 -->
         <template slot="action" slot-scope="{ row }"  >
          <el-link type="primary" v-permission="'SaleOutOrder_Update'" :underline="false" v-show="row.outStatus==1"  icon="el-icon-edit"  @click="openEdit(row,2)" >修改</el-link>
          <el-popconfirm v-show="row.outStatus==1 && row.deleted!=1" class="ele-action" title="确定要作废吗？"  @confirm="remove(row)" >
            <el-link type="danger" v-permission="'SaleOutOrder_Delete'"  slot="reference" :underline="false"  icon="el-icon-delete" >作废</el-link>
          </el-popconfirm>
          <el-link type="info"  :underline="false"  icon="el-icon-view"  @click="openEdit(row,3)" >详情</el-link>
        </template>
        </ele-pro-table>
      </el-card>
      <!-- 编辑弹窗 -->
      <saleoutorderEdit :data="current" :visible.sync="showEdit" @done="reload" :btnIndex="btnIndex"/>
      <!-- 导出数据 -->
      <ExportDialog ref="ExportDialog"></ExportDialog>
    </div>
  </template>
  
  <script>
    
    import { removePageTab } from '@/utils/page-tab-util';
    import ExportDialog from '@/components/Excel/ExportDialog'
    import saleoutorderEdit from './components/saleoutorder-edit.vue';
    import saleoutorderSearch from './components/saleoutorder-search.vue';
    import { QuerySaleoutorderByCondition,DeleteSaleoutorder } from '@/api/businesshandling/saleoutmanger';

    export default {
      name: 'Saleout',
      components: {saleoutorderEdit,saleoutorderSearch,ExportDialog},
      data() {
        return {
          // 表格列配置
          columns: [
            {columnKey: 'index', type: 'index', width: 50, align: 'center', showOverflowTooltip: true, fixed: 'left', label:'序号',},
            {prop: 'outOrderNo', label: '出库单号', width: 140, showOverflowTooltip: true, align: 'center',sortable:'outOrderNo',slot:'outOrderNo'},
            {prop: 'orderNo', label: '订单号', width: 140, showOverflowTooltip: true, align: 'center',sortable:'orderNo',},
            {prop: 'customerName', label: '客户名称', width: 120,  showOverflowTooltip: true, align: 'center',},
            {prop: 'orderAmount', label: '出库金额',  width: 140, showOverflowTooltip: true, align: 'center',},
            {prop: 'preAmount', label: '预收金额',  width: 140, showOverflowTooltip: true, align: 'center',},
            {prop: 'disAmount', label: '优惠金额', width: 140,  showOverflowTooltip: true,align: 'center',},
            {prop: 'recAmount', label: '收款金额',  width: 140, showOverflowTooltip: true, align: 'center',},
            {prop: 'currencyDesc', label: '币种', width: 80, showOverflowTooltip: true,align: 'center',},
            {
              prop: 'createDate', label: '日期',  width: 120, showOverflowTooltip: true, align: 'center',
              formatter: (row, column, cellValue) => {return this.$util.toDateString(cellValue,"YYYY-MM-DD");}
            },
            {prop: 'status', label: '状态', slot:'status',align: 'center',fixed: 'right',},
            {columnKey: 'action', label: '操作', width: 200, align: 'center', resizable: false,slot: 'action', hideInSetting: true, showOverflowTooltip: true,fixed: 'right',}
          ],
          orderNo:'',
          //是否详情
          btnIndex: '1',
          isDeleted: 0,
          fixedHeight:true,
          // 当前编辑数据
          current: null,
          // 是否显示编辑弹窗
          showEdit: false,
          // 导出列
          exportList: [
            { name: "出库单号", keys: "outOrderNo", status: true },
            { name: "订单号", keys: "orderNo", status: true },
            { name: "批次号", keys: "batchNo", status: true },
            { name: "客户名称", keys: "customerName", status: true },
            { name: "出库金额", keys: "orderAmount", status: true },
            { name: "预收金额", keys: "preAmount", status: true },
            { name: "优惠金额", keys: "disAmount", status: true },
            { name: "收款金额", keys: "recAmount", status: true },
            // { name: "收款账户", keys: "bankAccountDesc", status: true },
            { name: "币种", keys: "currencyDesc", status: true },
            // { name: "汇率", keys: "rate", status: true },
            { name: "日期", keys: "modifyDate", status: true },
            { name: "状态", keys: "statusDesc", status: true }
          ]
        };
      },
      computed: {
        // 表格高度
        tableHeight() {
          var fh = this.fixedHeight ? 'calc(100vh - 250px)' : void 0;

          return fh;
        },
        // 表格全屏时高度
        tableFullHeight() {
          var fh =  this.fixedHeight ? 'calc(100vh - 126px)' : null;

          return fh;
        },
      },
      created(){
        this.orderNo = this.$route.query.orderNo;
      },
      methods: {
        /* 表格数据源 */
        async datasource(params) {
          params.where.pageindex = params.page;
          params.where.pagesize = params.limit;
  
          //添加排序
          if(params.order.order == null || params.order.order == undefined){
            params.where.orderBys = [{ sort: "createDate", order: 1, }];
          }else{
            params.where.orderBys = [];
            params.where.orderBys.push({sort:params.sorter.column.sortable,order:params.order.order=='asc'?0:1});
          }

          if(!params.where.conditions)params.where.conditions= [];

          if(this.orderNo){
            params.where.conditions.push({
              key:"outOrderNo",
              operator: 0,
              value: this.orderNo,
            });
          }

          let delIndex = params.where.conditions.findIndex((g)=>{
            return g.key == 'deleted';
          })

          if(delIndex>=0){
            params.where.conditions.splice(delIndex,1)
            params.where.conditions.push({ key:"deleted", operator: 0, value: this.isDeleted})
          }else{
            params.where.conditions.push({
              key:"deleted",
              operator: 0,
              value: this.isDeleted,
            });
          }
        
          let data = await QuerySaleoutorderByCondition(params.where);
          
          if(data && data.items){
            data.items.map((m)=>{
              m.grey = this.isDeleted;
            })
          }

          this.$nextTick(() => {
            this.$refs.table.doLayout();
          });
          return {
              list:data.items,
              count:data.totalrecords,
          };
        },
  
        editState(){
          this.reload();
        },
        /* 表格渲染完成回调 */
        onDone({ data }) {
          this.supplierList = data;
        },
        /* 刷新表格 */
        reload(where) {
          
          this.$refs.table.reload({ where: where });

          this.$nextTick(() => {
            this.$refs.table.doLayout();
          });
        },
        
        /**
         * 显示编辑
         * @param row 当前行对像
         */
        openEdit(row,index) {
          // this.current = row;
          // this.btnIndex = index;
          // this.showEdit = true;
          removePageTab({ key: '/businesshandling/saleoutmanger/saleoutedit' });
          this.$nextTick(() => {
            this.$router.push({path: '/businesshandling/saleoutmanger/saleoutedit', query: { "saleoutorderId": row.id,"isUpdate":index} });
          });
        },
        /* 删除 */
        remove(row) {
          const loading = this.$loading({ lock: true });
          DeleteSaleoutorder(row.id)
            .then((msg) => {
              loading.close();
              this.$message.success(msg);
              this.reload();
            })
            .catch((e) => {
              loading.close();
              this.$message.error(e.message);
            });
        },
        /* 导出 */
        reportexcel(pams) {

          //初始化参数
          let params = {
            pagesize: 0,
            pageindex: 1,
            conditions: [],
            orderBys: [{ sort: "createDate", order: 1, }],
          };
          //加载进度条
          const loading = this.$loading({ lock: true });
          //重新请求查询
          this.$refs.table.doRequest(({ where }) => {
          if(where){
            params.orderBys = where.orderBys;
            params.pageindex = where.pageindex;
            params.conditions = where.conditions;
          }
          //查询数据
          QuerySaleoutorderByCondition(params)
            .then((data) => {
              loading.close();
              this.$refs.ExportDialog.showEdits = true
              this.$refs.ExportDialog.checkList = this.exportList
              this.$refs.ExportDialog.titleName = "销售出库单列表"
              this.$refs.ExportDialog.dataList = data.items;
            })
            .catch((e) => {
              loading.close();
              this.$message.error(e.message);
            });
          });
        },
        //单击单号
        clickbillNo(row){
          // 采购订单
          removePageTab({ key: '/businesshandling/saleordermanager' });
          this.$nextTick(() => {
            this.$router.push({path: '/businesshandling/saleordermanager', query: { "orderNo": row.orderNo } });
          });
        },
         /* 样式 */
         cellStyle(param){
          let cellStyle;
          const { row, column, rowIndex, columnIndex } = param;
          switch(row.grey == 1){
            case true:
              cellStyle = 'background-color: RGB(240,242,245)';
              break;
            default:
              cellStyle = '';
              break;
          }
          return cellStyle;
        },
      }
    };
  </script>
   <style lang="scss" scoped>
   /*总计字体颜色
   ::v-deep .el-table__footer-wrapper tbody td {
      color: var(--color-primary);
      cursor: pointer;
    }*/
  </style>